﻿using System;
using System.IO;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Xml.Serialization;

namespace Sripirom.ATS.Core.Messaging
{
    [MessageContract]
    public abstract class ChannelRequest
    {
        private ServiceMessageHeader _header; 
        public ChannelRequest()
        {
            _header = new ServiceMessageHeader();
        }

        [MessageHeader(Name = "Headers", Namespace = "http://www.kiatnakinbank.com/services/ATS/MessageHeader")]
        public ServiceMessageHeader Header { get { return _header; } set { _header = value; } }


    }
    [MessageContract]
    public abstract class ChannelResponse
    {
       
        private ServiceMessageHeader _header; 
        public ChannelResponse()
        {
           _header = new ServiceMessageHeader();
        }

        [MessageHeader(Name = "Headers", Namespace = "http://www.kiatnakinbank.com/services/ATS/MessageHeader")]
        public ServiceMessageHeader Header { get { return _header; } set { _header = value; } }

    }


    [DataContract(Name = "Output", Namespace = "http://www.kiatnakinbank.com/services/ATS/MessageOutput")]
    public class MessageOutput
    {
        private String _result;
        private String _errorReason;


        [DataMember(Name = "Result", Order = 1)]
        public String Result { get { return _result; } set { _result = value; } }
        [DataMember(Name = "ErrorReason", Order = 2)]
        public String ErrorReason { get { return _errorReason; } set { _errorReason = value; } }
    }

    [DataContract(Name = "Headers", Namespace = "http://www.kiatnakinbank.com/services/ATS/MessageHeader")]
    public class ServiceMessageHeader
    {
        private String _transactionId = "";
        private String _username = "";
        private String _password = "";
        private String _serviceName = "";
        private String _channelId = "";
        private String _transactionDate = "";
        private String _transactionTime = "";
        private String _version = "";
        private Decimal _sysServiceTransactionId;

        [DataMember(Name = "TransactionId", Order=1)]
        public String TransactionId { get { return _transactionId; } set { _transactionId = value; } }

        [DataMember(Name = "Username",Order=2)]
        public String Username { get { return _username; } set { _username = value; } }

        [DataMember(Name = "Password", Order=3)]
        public String Password { get { return _password; } set { _password = value; } }

        [DataMember(Name = "ServiceName", Order=4)]
        public String ServiceName { get { return _serviceName; } set { _serviceName = value; } }

        [DataMember(Name = "ChannelId", Order=5)]
        public String ChannelId { get { return _channelId; } set { _channelId = value; } }

        [DataMember(Name = "TransactionDate", Order=6)]
        public String TransactionDate { get { return _transactionDate; } set { _transactionDate = value; } }

        [DataMember(Name = "TransactionTime", Order=7)]
        public String TransactionTime { get { return _transactionTime; } set { _transactionTime = value; } }

        [DataMember(Name = "Version", Order = 8)]
        public String Version { get { return _version; } set { _version = value; } }

    }

    public class ServiceHeader : MessageHeader
    {
        private ServiceMessageHeader _header;
        public ServiceHeader(ServiceMessageHeader header)
        {
            _header = header;
        }
        private const string CUSTOM_HEADER_NAME = "Headers";
        private const string CUSTOM_HEADER_NAMESPACE = "http://www.kiatnakinbank.com/services/ATS/MessageHeader";

        public override string Name
        {
            get { return (CUSTOM_HEADER_NAME); }
        }

        public override string Namespace
        {
            get { return (CUSTOM_HEADER_NAMESPACE); }
        }

        protected override void OnWriteHeaderContents(
            System.Xml.XmlDictionaryWriter writer, MessageVersion messageVersion)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ServiceMessageHeader));
            StringWriter textWriter = new StringWriter();
            serializer.Serialize(textWriter, this);
            textWriter.Close();

            string text = textWriter.ToString();

            writer.WriteElementString(CUSTOM_HEADER_NAME, CUSTOM_HEADER_NAMESPACE, text.Trim());
        }

        public static ServiceMessageHeader ReadHeader(Message request)
        {
            Int32 headerPosition = request.Headers.FindHeader(CUSTOM_HEADER_NAME, CUSTOM_HEADER_NAMESPACE);
            if (headerPosition == -1)
                return null;

            XmlObjectSerializer serializer = new DataContractSerializer(typeof(ServiceMessageHeader), CUSTOM_HEADER_NAME, CUSTOM_HEADER_NAMESPACE, null, Int32.MaxValue, false, false, null);
    
            return request.Headers.GetHeader<ServiceMessageHeader>(headerPosition, serializer);

        }
    }
}
